--[[dev by.miao]]
local MessageBoxT = {
    BoxIcons = {
        ["Question"] = "http://www.roblox.com/asset/?id=8800441559",
        ["Error"] = "http://www.roblox.com/asset/?id=8800303348",
        ["Warning"] = "http://www.roblox.com/asset/?id=8800428538"
    }
}

local ID = 8801438982
local CurrentCamera
local plrs = game:GetService("Players")
local plr = game:GetService("Players").LocalPlayer
local TweenService = game:GetService("TweenService")
local inpService = game:GetService("UserInputService")
local Mouse = plr:GetMouse()

while not CurrentCamera do
    CurrentCamera = workspace.CurrentCamera
    task.wait()
end

local minimized = false
local offsetY = 0

local function ToBounds(X, Y, MW)
    if X - MW.Size.X.Offset / 2 < 0 then
        X = MW.Size.X.Offset / 2
    end
    if (MW.AbsoluteSize.Y - Y) - MW.Size.Y.Offset / 2 >= MW.AbsoluteSize.Y then
        Y = MW.Size.Y.Offset / 2
    end
    if X + MW.AbsoluteSize.X >= CurrentCamera.ViewportSize.X + MW.Size.X.Offset / 2 then
        X = CurrentCamera.ViewportSize.X - MW.AbsoluteSize.X + MW.Size.X.Offset / 2
    end
    if Y + MW.AbsoluteSize.Y + 35 >= CurrentCamera.ViewportSize.Y + MW.Size.Y.Offset / 2 then
        Y = CurrentCamera.ViewportSize.Y - MW.AbsoluteSize.Y - 35 + MW.Size.Y.Offset / 2
    end
    return UDim2.new(0, X, 0, Y)
end

local function Tween(object, time, properties)
    local info = TweenInfo.new(time, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
    TweenService:Create(object, info, properties):Play()
end

local function ApplyDrag(Component, MainWindow)
    local MouseDown = false
    local MB1 = Enum.UserInputType.MouseButton1
    local MM = Enum.UserInputType.MouseMovement
    local Position = Vector2.new()
    local InputBegan = Component.InputBegan:Connect(function(input)
        if input.UserInputType == MB1 then
            Component.BackgroundTransparency = 0
            Component["Box-Title"].TextColor3 = Color3.fromRGB(255, 255, 255)
            Component["Close"]["btn"].TextColor3 = Color3.fromRGB(255, 255, 255)
            MouseDown = true
            Position = Vector2.new(
                input.Position.X - MainWindow.AbsolutePosition.X - MainWindow.Size.X.Offset / 2,
                input.Position.Y - MainWindow.AbsolutePosition.Y - MainWindow.Size.Y.Offset / 2
            )
        end
    end)
    local InputEnded = Component.InputEnded:Connect(function(input)
        if input.UserInputType == MB1 then
            MouseDown = false
            Component.BackgroundTransparency = 1
            Component["Box-Title"].TextColor3 = Color3.fromRGB(0, 0, 0)
            Component["Close"]["btn"].TextColor3 = Color3.fromRGB(0, 0, 0)
        end
    end)
    local InputChanged = inpService.InputChanged:Connect(function(input)
        if MouseDown and input.UserInputType == MM then
            Tween(MainWindow, 0.1, { Position = ToBounds(Mouse.X - Position.X, Mouse.Y - Position.Y, MainWindow) })
        end
    end)
    MainWindow.AncestryChanged:Connect(function()
        if not MainWindow.Parent then
            InputBegan:Disconnect()
            InputEnded:Disconnect()
            if InputChanged.Connected then InputChanged:Disconnect() end
        end
    end)
end

function MessageBoxT.Show(option)
    option = typeof(option) == "table" and option or {}
    local MessageDescription = tostring(option.Description) and option.Description or "This is a Notification"
    local Options = tostring(option.MessageBoxButtons) and option.MessageBoxButtons or "OK"
    local MessageIcon = tostring(option.MessageBoxIcon) and option.MessageBoxIcon or "Warning"
    local ResultCallback = typeof(option.Result) == "function" and option.Result or function() end
    local MessageTitle = tostring(option.Text) and option.Text or ""
    local Duration = tonumber(option.time) or 3

    local GUI
    if game.CoreGui:FindFirstChild("Notifications") then 
        GUI = game.CoreGui:FindFirstChild("Notifications")
    else 
        GUI = Instance.new("ScreenGui")
        GUI.Name = "Notifications"
        GUI.ResetOnSpawn = false
        GUI.Parent = game.CoreGui
    end

    local MessageBoxTemplate = game:GetObjects("rbxassetid://" .. ID)[1]
    MessageBoxTemplate["UIScale"].Scale = 0
    local MessageBox = MessageBoxTemplate:Clone()
    MessageBox.Parent = GUI

    MessageBox["Message-Header"]["Box-Title"].Text = MessageTitle
    MessageBox["MessageDescription"].Text = MessageDescription

    MessageBox["Message-Icons"]["Error"].Image = MessageBoxT.BoxIcons["Error"]
    MessageBox["Message-Icons"]["Warning"].Image = MessageBoxT.BoxIcons["Warning"]
    MessageBox["Message-Icons"]["Question"].Image = MessageBoxT.BoxIcons["Question"]

    ApplyDrag(MessageBox["Message-Header"], MessageBox)

    local Icon = MessageBox["Message-Icons"]:FindFirstChild(MessageIcon)
    if Icon then
        Icon.Parent = MessageBox
        Icon.Visible = true
    end

    local Buttons = nil
    local Addup = 0

    if Options ~= "None" then
        Buttons = MessageBox["MessageBoxButtons"][Options]:Clone()
        Buttons.Visible = true
        Buttons.Parent = MessageBox
        Addup = 36
    else
        Addup = 6
    end

    if MessageBox["MessageDescription"].TextBounds.Y >= 16 then
        MessageBox["MessageDescription"].Position = UDim2.new(0, 48, 0, 42)
        Addup = Addup - 14
    end

    MessageBox.Size = UDim2.new(
        0,
        math.max(MessageBox["MessageDescription"].TextBounds.X + 100, 200),
        0,
        MessageBox["MessageDescription"].TextBounds.Y + 70 + Addup
    )

    local vpSize = CurrentCamera.ViewportSize
    local boxSize = MessageBox.Size
    local posX = vpSize.X - boxSize.X.Offset
    local posY = vpSize.Y - boxSize.Y.Offset
    MessageBox.Position = UDim2.fromOffset(posX, posY)

    if Buttons then
        for _, v in ipairs(Buttons:GetChildren()) do
            if v:IsA("TextButton") then
                v.MouseButton1Click:Connect(function()
                    ResultCallback(v.Text)
                    TweenService:Create(MessageBox["UIScale"], TweenInfo.new(0.1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {
                        Scale = 0
                    }):Play()
                    task.delay(0.1, function()
                        if MessageBox and MessageBox.Parent then
                            MessageBox:Destroy()
                        end
                    end)
                end)
            end
        end
    end

    TweenService:Create(MessageBox["UIScale"], TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {
        Scale = 1
    }):Play()

    task.delay(Duration - 0.5, function()
        local currentTransparency = 0
        while currentTransparency < 1 do
            task.wait(0.05)
            currentTransparency = math.min(1, currentTransparency + 0.1)
            pcall(function()
                if not MessageBox or not MessageBox.Parent then return end
                MessageBox.BackgroundTransparency = currentTransparency
                for _, obj in ipairs(MessageBox:GetDescendants()) do
                    if obj:IsA("ImageLabel") or obj:IsA("ImageButton") then
                        obj.ImageTransparency = currentTransparency
                    elseif obj:IsA("TextLabel") or obj:IsA("TextButton") then
                        obj.TextTransparency = currentTransparency
                    end
                end
            end)
        end
        pcall(function()
            if MessageBox and MessageBox.Parent then
                MessageBox:Destroy()
            end
        end)
    end)
end

return MessageBoxT